package controllers.service;

import java.util.List;

import javax.persistence.EntityManager;

import enumerators.Tables;
import models.domain.Customer;
import models.domain.User;
import models.persistence.JPAUtil;
import models.persistence.StaticQuerries;

public class ModifyPersonalDetailsService {

	/**
	 * Retrieves the customer
	 * 
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Customer retrieveCustomer(User user) {
		EntityManager em = JPAUtil.getCurrentEntityManager();
		List<Customer> listCustomers =(List<Customer>) (Object)StaticQuerries.getAllItems(em,Tables.Customer);
		for (Customer customer : listCustomers) {
			if (customer.getUser().getUserName() == user.getUserName()) {
				return customer;
			}
		}

		return null;
	}

	/**
	 * Modify details return false on failure
	 * 
	 * @param customer
	 * @return
	 */
	public boolean modifyDetails(Customer customer) {

		CustomerService customerService = new CustomerService();
		if (customerService.validateData(customer.getAFM(),customer.getE_mail())) {
			return false;
		} else {
			EntityManager em = JPAUtil.getCurrentEntityManager();
			em.merge(customer);
			return true;
		}

	}

}
